package cn.zhl.collector;

import cn.zhl.lambda.Person;

import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;

/**
 * @program: demos
 * @description: 自定义收集器
 * 注意：Characteristics.IDENTITY_FINISH  表示中间结果和返回结果相同
 * @author: 刘振华
 * @create: 2020-04-24 08:35
 **/
public class MySetCollector<T> implements Collector<T, Set<T>, List<T>> {

	public static void main(String[] args) {
		Person p111 = Person.builder().nj("一年级").bj("一班").zu("一组").name("a").age(9).score(85).build();
		Person p112 = Person.builder().nj("一年级").bj("一班").zu("二组").name("i").age(10).score(90).build();
		Person p113 = Person.builder().nj("一年级").bj("一班").zu("三组").name("c").age(11).score(95).build();
		Person p121 = Person.builder().nj("一年级").bj("二班").zu("一组").name("a").age(10).score(70).build();
		Person p122 = Person.builder().nj("一年级").bj("二班").zu("二组").name("e").age(9).score(80).build();
		Person p123 = Person.builder().nj("一年级").bj("二班").zu("三组").name("f").age(11).score(90).build();
		Person p211 = Person.builder().nj("二年级").bj("一班").zu("一组").name("g").age(11).score(100).build();
		Person p212 = Person.builder().nj("二年级").bj("一班").zu("二组").name("b").age(12).score(90).build();
		Person p213 = Person.builder().nj("二年级").bj("一班").zu("三组").name("i").age(12).score(90).build();
		List<Person> ps = Arrays.asList(p111, p112, p113, p121, p122, p123, p211, p212, p213);
		System.out.println(ps);
		ps.stream().collect(new MySetCollector<>()).forEach(System.out::println);
//        System.out.println(cs);
	}

	@Override
	public Supplier<Set<T>> supplier() {
		System.out.println("supplier");
		return HashSet::new;
	}

	@Override
	public BiConsumer<Set<T>, T> accumulator() {
		System.out.println("accumulator");
		return Set::add;
	}

	@Override
	public BinaryOperator<Set<T>> combiner() {
		System.out.println("combiner");
		return (a, b) -> {
			a.addAll(b);
			return a;
		};
	}

	@Override
	public Function<Set<T>, List<T>> finisher() {
		System.out.println("finisher");
		return a -> new ArrayList<>(a);
	}

	@Override
	public Set<Characteristics> characteristics() {
		return EnumSet.of(Characteristics.UNORDERED);
	}
}
